package com.hoshiibuy.sys.permission.controller;

import java.time.LocalDateTime;
import java.util.List;

import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiibuy.common.rpc.Response;
import com.hoshiibuy.sys.permission.entity.SysModels;
import com.hoshiibuy.sys.permission.entity.param.SysModelsInsertParam;
import com.hoshiibuy.sys.permission.entity.param.SysModelsQueryParam;
import com.hoshiibuy.sys.permission.entity.param.SysModelsUpdateParam;
import com.hoshiibuy.sys.permission.entity.vo.SysModelsDetailVO;
import com.hoshiibuy.sys.permission.entity.vo.SysModelsVO;
import com.hoshiibuy.sys.permission.service.ISysPermissionesService;
import com.hoshiibuy.sys.permission.service.impl.SysModelsServiceImpl;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 系统功能接口
 * 
 * @author Mr.wu
 *
 */
@Api(tags = "系统功能操作接口")
@RestController
@RequestMapping(value = "/sysModel")
public class SysModelController {

	@Autowired
	private SysModelsServiceImpl sysModelsService;

	@Autowired
	private ISysPermissionesService sysPermissionesService;

	@ApiOperation("新增一个系统功能")
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public Response addSysModel(@Valid SysModelsInsertParam param, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return Response.failed("参数有误:" + bindingResult.getFieldError().toString());
		}
		SysModels model = new SysModels();
		BeanUtils.copyProperties(param, model);
		// TODO 只接受指定参数
		return sysModelsService.addSysModel(model);
	}

	@ApiOperation(value = "删除一个系统功能", notes = "将解除具体权限与功能的关联")
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public Response deleteSysModel(@RequestParam String id) {
		if (StringUtils.isBlank(id)) {
			return Response.failed("id不能为空");
		}
		return sysModelsService.deleteSysModel(id);
	}

	@ApiOperation("更新系统功能")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public Response updateSysModel(SysModelsUpdateParam param) {
		SysModels model = new SysModels();
		BeanUtils.copyProperties(param, model);
		// TODO 接收指定参数
		model.setUpdateTime(LocalDateTime.now());
		return sysModelsService.updateSysModel(model);
	}

	@ApiOperation("获取一个系统功能详情")
	@RequestMapping(value = "/getDetail", method = RequestMethod.GET)
	public Response<SysModelsDetailVO> getSysModelDetail(@RequestParam String id) {
		if (StringUtils.isBlank(id)) {
			return Response.failed("id不能为空");
		}
		return sysModelsService.getSysModelDetail(id);
	}

	@ApiOperation(value = "获取系统功能列表", notes = "不包括权限树")
	@RequestMapping(value = "getList", method = RequestMethod.GET)
	public Response<List<SysModelsVO>> getSysModelList(@Valid SysModelsQueryParam param, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return Response.failed("参数有误:" + bindingResult.getFieldError().toString());
		}
		QueryWrapper<SysModels> queryWrapper = param.getQueryWrapper();
		return sysModelsService.getSysModelList(queryWrapper);
	}

	@ApiOperation("分页查询系统功能")
	@RequestMapping(value = "/getPage", method = RequestMethod.GET)
	public Response<IPage<SysModelsVO>> getModelPage(@Valid SysModelsQueryParam param, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return Response.failed("参数有误:" + bindingResult.getFieldError().toString());
		}
		QueryWrapper<SysModels> queryWrapper = param.getQueryWrapper();

		IPage<SysModels> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));

		return sysModelsService.getPage(page, queryWrapper);
	}

}
